//
//  WDEnvironment.m
//  Woody
//
//  Created by Alean on 2022/11/29.
//

#import "WDEnvironment.h"

static NSString * const kHostkey = @"Hostkey";
static NSString * const kEnvironmentkey = @"Environmentkey";

static NSString *hostKey;
static NSString *environmentkey;

@interface WDEnvironment()
@property (nonatomic, strong, class, readwrite) NSString *host;
@property (nonatomic, copy, readwrite) NSDictionary *configDic;
@property (nonatomic, assign, readwrite) int target;
@end

@implementation WDEnvironment

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static WDEnvironment *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance ;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [WDEnvironment shareInstance];
}

- (id)copyWithZone:(struct _NSZone *)zone{
    return [WDEnvironment shareInstance];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        hostKey = [NSString stringWithFormat:@"%@_%@", kHostkey, WDProjectEnvironment()];
        environmentkey = [NSString stringWithFormat:@"%@_%@", kEnvironmentkey, WDProjectEnvironment()];
    }
    return self;
}

- (void)setupEnvironmentWithTarget:(int)target {
    self.target = target;
    
    WDEnvironment.host = self.configDic[@"hosts"][WDEnvironment.currentEnvironment];
}

- (NSDictionary *)configDic {
    if (_configDic == nil) {
        //    NSBundle *bundle = [NSBundle mainBundle];
        NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"WDLogicBehind.bundle"];
        NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
        
        NSString *path = @"";
        if (_target == 0) {
            path = [bundle pathForResource:@"Config-Dev" ofType:@"plist"];
        } else {
            path = [bundle pathForResource:@"Config" ofType:@"plist"];
        }
        _configDic = [NSDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"config = %@", _configDic);
    }
    return _configDic;
}

+ (NSString *)host {
    NSString *current = [[NSUserDefaults standardUserDefaults] objectForKey:hostKey];
    if (current) {
        
    } else {
        if ([WDEnvironment shareInstance].target == 0) {
            current = @"https://backend-dev.shwoody.com";
        } else {
            current = @"https://backend.shwoody.com";
        }
    }
    return current;
}

+ (void)setHost:(NSString *)host {
    [[NSUserDefaults standardUserDefaults] setObject:host forKey:hostKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)currentEnvironment {
    NSString *current = [[NSUserDefaults standardUserDefaults] objectForKey:environmentkey];
    if (current) {

    } else {
        if ([WDEnvironment shareInstance].target == 0) {
            current = @"dev";
        } else {
            current = @"prd";
        }
    }
    return current;
}

+ (void)setCurrentEnvironment:(NSString *)currentEnvironment {
    [[NSUserDefaults standardUserDefaults] setObject:currentEnvironment forKey:environmentkey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    WDEnvironment.host = [WDEnvironment shareInstance].configDic[@"hosts"][currentEnvironment];
}

@end
